const app = getApp();
const https = require('../../utils/https');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    couponList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this,
    login_status = wx.getStorageSync('login_status');
  if (!login_status || login_status <= 0) {
    wx.showLoading({
      title: '请先进行登陆',
    })
    setTimeout(() => {
      wx.hideLoading({
        success: (res) => {
          wx.navigateTo({
            url: '/pages/grant/grant',
          })
        },
      })
    }, 1500);
  } else {
    that.getList();
  }
  },
  /**
   * @xk 获取优惠券列表
   */
  getList: function(){
    var that = this,
        user_id = wx.getStorageSync('user_id');
    https.post('getUserCoupon',{user_id: user_id},false,'正在获取中').then(res=>{
      that.setData({
        couponList: res
      })
    })
  },
  /**
   * @xk 使用优惠券
   */
  userCoupon: function(obj){
    var that = this,
        user_coupon_id = obj.currentTarget.dataset.user_coupon_id,
        order_type = app.globalData.order_type,
        cart_ids = app.globalData.cart_id;
    if(cart_ids != '' && cart_ids != null){
      app.globalData.user_coupon_id = user_coupon_id;
      wx.navigateBack({
        delta: 0,
      })
    }else if(order_type != '' && order_type == 'gbook'){
      app.globalData.user_coupon_id = user_coupon_id;
      wx.navigateBack({
        delta: 0,
      })
    }else{
      wx.navigateTo({
        url: '/pages/register_appointment/register_appointment',
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow();
  },

})